package com.rw.test.restful;

import com.rw.engine.ip.anno.IpLimit;
import com.rw.engine.ip.anno.IpParam;
import com.rw.engine.ip.enumeration.FilterMode;
import com.rw.engine.ip.enumeration.MatchMode;
import com.rw.engine.ip.interceptor.DoNothingIpFilterRule;
import com.rw.test.vo.ResultVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * IP拦截器测试
 */
@RestController
@RequestMapping("/RWdaze/IpTestController")
@IpLimit(ip = "127.0.0.*", filterMode = FilterMode.BLACKLIST)
public class IpTestController {
    /**
     * IP白名单验证测试
     * IPParam注解获取IP参数测试
     */
    @GetMapping("/pass")
    @IpLimit(ip = "127.0.0.1", filterMode = FilterMode.WHITELIST)
    public ResultVO<String> passIpTest(@IpParam String ip) {
        return new ResultVO<>(0, "success", ip);
    }

    /**
     * IP黑名单拦截测试
     * 通配符测试
     */
    @GetMapping("/intercept")
    @IpLimit(ip = "127.0.0.*", filterMode = FilterMode.BLACKLIST)
    public ResultVO<?> interceptIpTest() {
        return new ResultVO<>(1, "Not Intercept!");
    }

    /**
     * IP黑名单拦截，正则表达式匹配测试
     */
    @GetMapping("/regex")
    @IpLimit(ip = "\\d+\\.0.0.\\d+", filterMode = FilterMode.BLACKLIST, matchMode = MatchMode.REGEX)
    public ResultVO<?> regexIpTest() {
        return new ResultVO<>(1, "Not Intercept!");
    }

    /**
     * IP黑名单拦截，基于方法上的测试
     */
    @GetMapping("/global")
    public ResultVO<?> globalIpTest() {
        return new ResultVO<>(1, "Not Intercept!");
    }

    /**
     * IP不拦截
     * 基于优先级的测试
     */
    @GetMapping("/single")
    @IpLimit(ipFilterRule = DoNothingIpFilterRule.class)
    public ResultVO<?> singleIpTest(@IpParam String ip) {
        return new ResultVO<>(0, "success", ip);
    }
}
